package com.yanqu.road.dao.impl.activity.silkroad;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.activity.silkroad.config.SilkRoadEventConfig;
import com.yanqu.road.entity.activity.silkroad.config.SilkRoadItemConfig;
import com.yanqu.road.entity.activity.silkroad.config.SilkRoadPlaceConfig;
import com.yanqu.road.entity.activity.silkroad.config.SilkRoadSeaConfig;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class SilkRoadConfigDaoImpl extends TempDao {
    public List<SilkRoadPlaceConfig> getSilkRoadPlaceList(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        List<SilkRoadPlaceConfig> list = new ArrayList<>();
        String sql = "select * from t_s_activity_silkroad_place where activityId=? order by id asc";

        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setInt(1, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                SilkRoadPlaceConfig data = new SilkRoadPlaceConfig();
                data.setActivityId(rs.getInt("activityId"));
                data.setId(rs.getInt("id"));
                data.setIcon(rs.getString("icon"));
                data.setDesc(rs.getString("desc"));
                data.setName(rs.getString("name"));
                data.setEventDesc(rs.getString("eventDesc"));
                data.setPlaceCoordinate(rs.getString("placeCoordinate"));
                data.setRouteCoordinateLists(rs.getString("routeCoordinateLists"));
                data.setRewards(rs.getString("rewards"));
                data.setSpecialRewards(rs.getString("specialRewards"));

                if (!StringUtils.isNullOrEmpty(data.getRouteCoordinateLists())) {
                    List<String> routList = StringUtils.stringToStringList(data.getRouteCoordinateLists(), "\\|");
                    data.setRouteSize(routList.size());
                }
                list.add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

    public Map<Integer, SilkRoadEventConfig> getSilkRoadEventMap(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, SilkRoadEventConfig> dataMap = new ConcurrentHashMap<>();
        String sql = "select * from t_s_activity_silkroad_event where activityId=? order by id asc";

        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setInt(1, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                SilkRoadEventConfig data = new SilkRoadEventConfig();
                data.setActivityId(rs.getInt("activityId"));
                data.setId(rs.getInt("id"));
                data.setIcon(rs.getString("icon"));
                data.setEventType(rs.getInt("eventType"));
                data.setEventDesc(rs.getString("eventDesc"));
                data.setRewards(rs.getString("rewards"));
                data.setSpecialRewards(rs.getString("specialRewards"));

                dataMap.put(data.getId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public Map<Integer, SilkRoadSeaConfig> getSilkRoadSeaMap(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, SilkRoadSeaConfig> dataMap = new ConcurrentHashMap<>();
        String sql = "select * from t_s_activity_silkroad_sea where activityId=? order by id asc";

        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setInt(1, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                SilkRoadSeaConfig data = new SilkRoadSeaConfig();
                data.setActivityId(rs.getInt("activityId"));
                data.setId(rs.getInt("id"));
                data.setType(rs.getInt("type"));
                data.setMapId(rs.getInt("mapId"));
                data.setWeights(rs.getString("weights"));
                data.setRewards(rs.getString("rewards"));
                data.setRandmoNum(rs.getInt("num"));
                data.setIcon(rs.getString("icon"));
                data.setName(rs.getString("name"));
                data.setQuality(rs.getInt("quality"));
                data.setRange(rs.getString("range"));
                data.setCoordinate(rs.getString("coordinate"));
                data.setMoveType(rs.getInt("moveType"));
                data.setMoveRange(rs.getInt("moveRange"));
                data.setSpeed(rs.getInt("speed"));
                data.setRetractSpeed(rs.getInt("retractSpeed"));

                dataMap.put(data.getId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public Map<Integer, SilkRoadItemConfig> getSilkRoadItemMap(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, SilkRoadItemConfig> dataMap = new ConcurrentHashMap<>();
        String sql = "select * from t_s_activity_silkroad_item where activityId=? order by id asc";

        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setInt(1, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                SilkRoadItemConfig data = new SilkRoadItemConfig();
                data.setActivityId(rs.getInt("activityId"));
                data.setId(rs.getInt("id"));
                data.setIcon(rs.getString("icon"));
                data.setQuality(rs.getInt("quality"));
                data.setDesc(rs.getString("desc"));
                data.setName(rs.getString("name"));
                data.setAchievementRewards(rs.getInt("achievementRewards"));
                data.setIsAnimation(rs.getInt("isAnimation"));

                dataMap.put(data.getId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }



}
